# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=klfc
pkgver=1.5.6
pkgrel=1
pkgdesc="Keyboard Layout Files Creator"
url="https://github.com/39aldo39/klfc"
arch=""  # failed to build, limited by ghc / cabal
license="GPL-3.0"
makedepends="cabal ghc"
subpackages="$pkgname-doc"
source="https://github.com/39aldo39/klfc/archive/v$pkgver/klfc-$pkgver.tar.gz
	cabal.config
	"
options="!check"  # it seems that no tests are provided

# Cabal seems to be built without sandbox, moving the cabal-dir into src.
export CABAL_DIR="$srcdir/.cabal"

cabal_update() {
	abuild unpack

	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.config with version constraints.
	cabal update

	cd "$builddir"
	cabal freeze --shadow-installed-packages

	# Add version tag at the first line.
	sed -i "1i--$pkgver" cabal.config

	mv cabal.config "$startdir"/

	cd "$startdir"
	abuild checksum || die "Failed to update checksum, run 'abuild checksum' manually"
}

prepare() {
	default_prepare

	if [ "$(head -n 1 "$srcdir"/cabal.config)" != "--$pkgver" ]; then
		die "Requirements file is outdated, run 'abuild cabal_update'"
	fi
	ln -sf "$srcdir"/cabal.config "$builddir"/cabal.config

	cabal update
}

build() {
	cabal configure \
		--prefix='/usr' \
		--bindir='$prefix/bin' \
		--sysconfdir='/etc' \
		--enable-executable-stripping \
		--enable-split-sections \
		--offline \
		--jobs=${JOBS:-2} \
		--ghc-options="${CFLAGS:+$(echo " $CFLAGS" | sed 's/ / -optc/g')}" \
		--ghc-options="${LDFLAGS:+$(echo " $LDFLAGS" | sed 's/ / -optl/g')}" \
		--ghc-option='-split-sections'
	cabal build
}

package() {
	local docdir="$pkgdir/usr/share/doc/$pkgname"

	install -D -m755 dist-newstyle/build/*/*/*/build/klfc/klfc \
		"$pkgdir"/usr/bin/klfc

	mkdir -p "$docdir"
	cp -r doc/* "$docdir"/
	cp -r examples "$docdir"/
}

sha512sums="
218190f53b5bfe4f438ef6831cabbccc3359ace821e4ce1937515bc08bb8ae459b5b1361c0ce42f839cfd657e76f089d3f088c5c4e04efdb783a5c2ee52eccf5  klfc-1.5.6.tar.gz
368d0ff3a6ba3fdc8deec13868240b55cccfd674783bd8837bad2338a424ae30a9978f266e8d418648bb878afde6ddd381f2ad6d9793e4a52d4f60c217a2b082  cabal.config
"
